package za.co.dylanjaneke.airdownloader.command
{
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import za.co.dylanjaneke.airdownloader.controller.data.ConnectionManager;
	import za.co.dylanjaneke.airdownloader.events.DownloadAllLinksEvent;
	import za.co.dylanjaneke.airdownloader.events.ShowDownloadQueueEvent;
	import za.co.dylanjaneke.airdownloader.model.DownloadQueuePM;
	import za.co.dylanjaneke.airdownloader.model.StatusPM;
	import za.co.dylanjaneke.airdownloader.model.WeekViewPM;
	import za.co.dylanjaneke.airdownloader.model.vo.DayEntryVO;
	import za.co.dylanjaneke.airdownloader.model.vo.FileDownloadVO;
	import za.co.dylanjaneke.airdownloader.model.vo.ShowVO;
	import za.co.dylanjaneke.airdownloader.util.ViLogger;

	/**
	 * Author: Dylan Janeke.
	 */
	[Name("downloadAllLinksCommand")]
	public class DownloadAllLinksCommand
	{
		private static const log:ViLogger= 
			new ViLogger(DownloadAllLinksCommand);
		
		[Inject]
		public var downloadQueueScreen:DownloadQueuePM;
		
		[Inject]
		public var weekViewPM:WeekViewPM;
		
		[Inject]
		public var statusPM:StatusPM;
		
		[Inject]
		public var repo:ConnectionManager;
		
		private var numLinks:int= 0;
		private var numLinksPersisted:int= 0;
		
		[Observer]
		public function handleDownloadEpisodesEvent(event:DownloadAllLinksEvent): void
		{
			numLinks= 0;
			numLinksPersisted= 0;
			
			var links:String="";
			
			var downloads:ArrayCollection=
				new ArrayCollection();
			
			for each(var day:DayEntryVO in weekViewPM.shows)
			{
				for each(var show:ShowVO in day.shows )
				{
					if(show.children.length>0)
					{
						for each(var lnk:ShowVO in show.children)
						{
							if(lnk.ticked)
							{
								//links+= lnk.label + "\n";
								
								var fileDownload:FileDownloadVO=
									new FileDownloadVO();
									
								fileDownload.linkName= 
									show.showName+
									" [S"+show.seasonNumber+"E"+show.episodeNumber+"] " + 
									show.episodeName;
								fileDownload.trashed= false;
								fileDownload.paused= true;
								fileDownload.url= lnk.label;
								
								downloads.addItem(fileDownload);					
								
								numLinks++;	
							}
						}
					}
				}
			}
			
			//actually add them
			for each(var download:FileDownloadVO in downloads)
			{
				repo.executeStatement(
					FileDownloadVO.createInsert(download, repo.createStatement()), onFileDownloadPersisted);
			}
			
			if(numLinks==0)
			{
				Alert.show("No links have been selected to download","No Links Selected");
				GlobalEventDispatcher.raiseEvent(new ShowDownloadQueueEvent());
			}
			else
			{	
				statusPM.status= numLinks + " download link" + (numLinks==1?"":"s") + " added to download queue.";
			}
		}
		
		protected function onFileDownloadPersisted(event:SQLEvent): void
		{
			log.info("something added ["+numLinksPersisted+","+numLinks+"]");
			if(numLinksPersisted== (numLinks-1))
			{
				GlobalEventDispatcher.raiseEvent(new ShowDownloadQueueEvent());
			}
			
			numLinksPersisted++;
		}
	}
}